//
//  WPRightManageFoootView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/6.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPRightManageFooterView.h"

@interface WPRightManageFooterView ()
@property (nonatomic, strong) UILabel * introLabel;/**< 说明文本 */
@property (nonatomic, strong) UIButton * transBtn;/**< 转让按钮 */
@property (nonatomic, strong) UIButton * spreadBtn;/**< 伸展按钮 */

- (void)initializeAppearance;/**< 一般界面初始化 */
@end

@implementation WPRightManageFooterView
- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, 300*DHFlexibleHorizontalMutiplier(), 110*DHFlexibleHorizontalMutiplier());
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.introLabel];
    [self addSubview:self.transBtn];
    if ([[UserModel defaultUser].memberRankNumber integerValue] == 1) {
        _transBtn.backgroundColor = [UIColor grayColor];
    }
    [self addSubview:self.spreadBtn];
}
#pragma mark - responds events
- (void)respondsToTransBtn {
    if (self.delegate && [_delegate respondsToSelector:@selector(actionOnTransBtn)]) {
        [_delegate actionOnTransBtn];
    }
}
- (void)respondsToSpreadBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    NSLog(@"%hhd", sender.selected);
    if (sender.selected) {
        [self.transBtn removeFromSuperview];
        self.introLabel.frame = CGRectMake(5*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 260*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
    } else {
        [self addSubview:self.transBtn];
        self.introLabel.frame = CGRectMake(5*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.transBtn.frame)+10*DHFlexibleHorizontalMutiplier(), 260*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
    }
    self.spreadBtn.frame = CGRectMake(140*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.introLabel.frame), 40*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
    if (self.delegate && [_delegate respondsToSelector:@selector(actionOnSpreadBtn:)]) {
        if (sender.selected) {
            [_delegate actionOnSpreadBtn:YES];
        } else {
            [_delegate actionOnSpreadBtn:NO];
        }
    }
}
#pragma mark - private methods
- (NSMutableAttributedString *)configAttributtedStringWithString:(NSString *)str {
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"* %@", str]];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(242, 106, 89, 1) range:NSMakeRange(0, 1)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(141, 141, 141, 1) range:NSMakeRange(1, attributedStr.length-1)];
    return attributedStr;
}
#pragma mark - getter
- (UILabel *)introLabel {
    if (!_introLabel) {
        _introLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(5*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.transBtn.frame)+10*DHFlexibleHorizontalMutiplier(), 260*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont boldSystemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.attributedText = [self configAttributtedStringWithString:@"票务项目，所有产品都默认为自助取票"];
            lab;
        });
    }
    return _introLabel;
}
- (UIButton *)transBtn {
    if (!_transBtn) {
        _transBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(120*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier(), 75*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier());
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            [btn setImage:IMAGE_NAME(@"iconfont-zhaiquanzhuanrangicon") forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToTransBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _transBtn;
}
- (UIButton *)spreadBtn {
    if (!_spreadBtn) {
        _spreadBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(130*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.introLabel.frame), 60*DHFlexibleHorizontalMutiplier(), 20*DHFlexibleHorizontalMutiplier());
            [btn setImage:IMAGE_NAME(@"iconfont-xiala-xq.png") forState:UIControlStateNormal];
            [btn setImage:IMAGE_NAME(@"iconfont-xiala-fangx") forState:UIControlStateSelected];
            btn.layer.cornerRadius = 3*DHFlexibleHorizontalMutiplier();
            btn.layer.masksToBounds = YES;
            [btn addTarget:self action:@selector(respondsToSpreadBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _spreadBtn;
}
@end
